import path from "path";
import fs from "fs";
import zipdir from "zip-dir";
export default {
    /**
     * import.meta
     * @param {*} meta 
     */
    meta(meta) {
        let url;
        console.log('OS sep:', path.sep);
        if (path.sep == '/') {//linux
            console.log('url:', meta.url);
            url = meta.url;
            // url = meta.url.replace("file:///", '');
        } else {//win
            url = meta.url.replace("file:///", '');
        }
        let uesrhome = process.env.HOME || process.env.USERPROFILE;
        let normal = path.resolve(url);
        let parseinfo = path.parse(normal);
        meta.home = uesrhome;
        meta.info = parseinfo;
        meta.cwd = process.cwd();
        meta.cachedir = path.resolve(meta.home + '/cache');
        fs.mkdirSync(meta.cachedir, { recursive: true });
        meta.__dirname = parseinfo.dir;
        meta.__filename = normal;
        console.log('meta:', meta);
        return meta;

    },
    async zipdir(dir, file) {
        console.log('zip start:', dir, file);
        let buffer = await zipdir(dir, { saveTo: file });
        console.log('zip ok:', this.size(buffer.length));
        return { size: this.size(buffer.length) }
    },
    size(size, deep = 0, format = ['K', 'M', 'G', 'T']) {
        let fsize = size / 1024;
        if (fsize > 1024) {
            return this.size(fsize, ++deep);
        }
        return fsize.toFixed(2) + format[deep];
    }
}